import exceptions.LexicalException;
import exceptions.OberonException;
import flowchart.*;
import flowchart.Module;
import parser.MyParser;
import parser.ParserFactory;
import scanner.MyScanner;
import scanner.ScannerFactory;

import java.io.FileNotFoundException;
import java.io.IOException;

/**
 * 测试驱动程序
 */
public class TestDrive {
    /**
     * 研究流程图的接口使用
     */
    static void test() {
        // 新建模块
        Module module = new Module("moduleName");
        // 向模块中新建一个函数，并返回
        Procedure procedure = module.add("procedure1");
        // if 语句
        IfStatement ifStatement = new IfStatement("IfStatement");
        ifStatement.getTrueBody().add(new PrimitiveStatement("ifTrue"));
        ifStatement.getFalseBody().add(new PrimitiveStatement("ifFalse"));
        // while 语句
        WhileStatement whileStatement = new WhileStatement("while");
        whileStatement.getLoopBody().add(new PrimitiveStatement("whileloop"));
        // 基本语句
        PrimitiveStatement statement = new PrimitiveStatement("PrimitiveStatement");
        // 将语句加入函数体中
        procedure.add(ifStatement);
        procedure.add(whileStatement);
        procedure.add(statement);
        // 第二个函数
        procedure = module.add("p2");
        // 语句序列
        StatementSequence statementSequence = new StatementSequence();
        // 向语句序列加入语句
        statementSequence.add(new PrimitiveStatement("1"));
        statementSequence.add(new PrimitiveStatement("2"));
        statementSequence.add(new PrimitiveStatement("3"));
        // 将语句序列加入函数体
        procedure.add(statementSequence);
        // 绘图
        module.show();
    }

    /**
     * 主函数
     */
    public static void main(String[] args) {
        if (args.length == 0) {
            String test = "../ex1/testcases/test.obr";
//            lex(test);
            parse(test);
        } else {
            for (String fileName : args) {
//                lex(fileName);
                parse(fileName);
            }
        }
    }

    /**
     * 对指定文件进行词法测试
     *
     * @param fileName 指定文件
     */
    public static void lex(String fileName) {
        try {
            MyScanner scanner = ScannerFactory.makeScanner(fileName);
            while (scanner.hasNext()) {
                System.out.println(scanner.getNext().toString());
            }
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (LexicalException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 对指定文件进行语法测试、语义测试，绘制流程图
     *
     * @param fileName 指定文件
     */
    public static void parse(String fileName) {
        try {
            MyParser parser = ParserFactory.makeParser(fileName);
            parser.parse();
            System.out.println("语法分析成功");
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (LexicalException e) {
            throw new RuntimeException(e);
        } catch (OberonException e) {
            throw new RuntimeException(e);
        }

    }
}
